metadata name = 'CDN Profiles AFD Endpoints'
metadata description = 'This module deploys a CDN Profile AFD Endpoint.'

@description('Required. The name of the AFD Endpoint.')
param name string

@description('Conditional. The name of the parent CDN profile. Required if the template is used in a standalone deployment.')
param profileName string

@description('Optional. The location of the AFD Endpoint.')
param location string = resourceGroup().location

@description('Optional. The tags of the AFD Endpoint.')
param tags object?

@description('Optional. Indicates the endpoint name reuse scope. The default value is TenantReuse.')
@allowed([
  'NoReuse'
  'ResourceGroupReuse'
  'SubscriptionReuse'
  'TenantReuse'
])
param autoGeneratedDomainNameLabelScope string = 'TenantReuse'

@description('Optional. Indicates whether the AFD Endpoint is enabled. The default value is Enabled.')
@allowed([
  'Enabled'
  'Disabled'
])
param enabledState string = 'Enabled'

@description('Optional. The list of routes for this AFD Endpoint.')
param routes routeType[]?

resource profile 'Microsoft.Cdn/profiles@2025-04-15' existing = {
  name: profileName
}

resource afdEndpoint 'Microsoft.Cdn/profiles/afdEndpoints@2025-04-15' = {
  name: name
  parent: profile
  location: location
  tags: tags
  properties: {
    autoGeneratedDomainNameLabelScope: autoGeneratedDomainNameLabelScope
    enabledState: enabledState
  }
}

module afdEndpoint_routes 'route/main.bicep' = [
  for route in (routes ?? []): {
    name: '${uniqueString(deployment().name, route.name)}-Profile-AfdEndpoint-Route'
    params: {
      name: route.name
      profileName: profile.name
      afdEndpointName: afdEndpoint.name
      cacheConfiguration: route.?cacheConfiguration
      customDomainNames: route.?customDomainNames
      enabledState: route.?enabledState
      forwardingProtocol: route.?forwardingProtocol
      httpsRedirect: route.?httpsRedirect
      linkToDefaultDomain: route.?linkToDefaultDomain
      originGroupName: route.?originGroupName
      originPath: route.?originPath
      patternsToMatch: route.?patternsToMatch
      ruleSets: route.?ruleSets
      supportedProtocols: route.?supportedProtocols
    }
  }
]

@description('The name of the AFD Endpoint.')
output name string = afdEndpoint.name

@description('The resource id of the AFD Endpoint.')
output resourceId string = afdEndpoint.id

@description('The name of the resource group the endpoint was created in.')
output resourceGroupName string = resourceGroup().name

@description('The location the resource was deployed into.')
output location string = afdEndpoint.location

@description('The list of routes assigned to the AFD endpoint.')
output routes array = routes ?? []

@description('The host name of the AFD endpoint.')
output frontDoorEndpointHostName string = afdEndpoint.properties.hostName

// =============== //
//   Definitions   //
// =============== //

@export()
@description('The type of the route.')
type routeType = {
  @description('Required. The name of the route.')
  name: string

  @description('Optional. The caching configuration for this route. To disable caching, do not provide a cacheConfiguration object.')
  cacheConfiguration: resourceInput<'Microsoft.Cdn/profiles/afdEndpoints/routes@2025-04-15'>.properties.cacheConfiguration?

  @description('Optional. The names of the custom domains.')
  customDomainNames: string[]?

  @description('Optional. Whether to enable use of this rule.')
  enabledState: 'Enabled' | 'Disabled' | null

  @description('Optional. The protocol this rule will use when forwarding traffic to backends.')
  forwardingProtocol: 'HttpOnly' | 'HttpsOnly' | 'MatchRequest' | null

  @description('Optional. Whether to automatically redirect HTTP traffic to HTTPS traffic.')
  httpsRedirect: 'Enabled' | 'Disabled' | null

  @description('Optional. Whether this route will be linked to the default endpoint domain.')
  linkToDefaultDomain: 'Enabled' | 'Disabled' | null

  @description('Required. The name of the origin group.')
  originGroupName: string

  @description('Optional. A directory path on the origin that AzureFrontDoor can use to retrieve content from, e.g. contoso.cloudapp.net/originpath.')
  originPath: string?

  @description('Optional. The route patterns of the rule.')
  patternsToMatch: resourceInput<'Microsoft.Cdn/profiles/afdEndpoints/routes@2025-04-15'>.properties.patternsToMatch?

  @description('Optional. The names of the rule sets of the rule.')
  ruleSets: string[]?

  @description('Optional. The supported protocols of the rule.')
  supportedProtocols: resourceInput<'Microsoft.Cdn/profiles/afdEndpoints/routes@2025-04-15'>.properties.supportedProtocols?
}
